package com.lemon.smartsafety.ui.activity.user;

import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ldzs.recyclerlibrary.Mode;
import com.ldzs.recyclerlibrary.PullToRefreshRecyclerView;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.user.ScoreInfoVo;
import com.lemon.smartsafety.entity.user.ScoreLogVo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.UserServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.ui.adapter.user.MyScoreAdapter;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Cooliean on 16/4/9.
 */
public class MyScoreActivity extends SwipeBackActivity {


    @Bind(R.id.pullToRefreshRecyclerView)
    PullToRefreshRecyclerView pullToRefreshRecyclerView;
    @Bind(R.id.multiStateView)
    MultiStateView            multiStateView;


    private ScoreInfoVo mScoreInfoVo;

    private MyScoreAdapter mScoreAdapter;

    private List<ScoreLogVo> mItems = new ArrayList<>();


    @Override
    protected void initTitle() {
        setBarTitle(R.string.draw_score);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(MyScoreActivity.this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_list_refresh;
    }

    private void initRecyclerView(){
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mScoreAdapter = new MyScoreAdapter(mItems);
        pullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);
        pullToRefreshRecyclerView.setLayoutManager(manager);
        pullToRefreshRecyclerView.setOnPullUpToRefreshListener(new PullToRefreshRecyclerView.OnPullUpToRefreshListener() {
            @Override
            public void onRefresh() {
                getNetData();
            }
        });
        pullToRefreshRecyclerView.setAdapter(mScoreAdapter);
    }


    @Override
    protected void initViews() {
        super.initViews();
        initRecyclerView();
        multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        multiStateView.setErrorRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                pullToRefreshRecyclerView.beginRefreshing();
            }
        });
        pullToRefreshRecyclerView.beginRefreshing();
    }

    private void getNetData() {
        UserServices.init().getMyScoreList();
    }



    /**
     * http处理请求的处理
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HttpCallbackEvent<ScoreInfoVo> event) {
        if (EventTag.TAG_MY_SCORE.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                pullToRefreshRecyclerView.onRefreshComplete();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.hidden();
                    mScoreInfoVo = event.getObj();
                    if (null != mScoreInfoVo) {
                        setHeadView(mScoreInfoVo);
                        mItems.clear();
                        mItems.addAll(mScoreInfoVo.getScoreLogList());
                        mScoreAdapter.notifyDataSetChanged();
                    }
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                    multiStateView.setErrorTip(event.getMsg());
                }
            }
        }
    }

    private void setHeadView(ScoreInfoVo scoreInfoVo) {
        if (scoreInfoVo == null) {
            return;
        }
        for (int i = 0; i < pullToRefreshRecyclerView.getHeaderViewCount(); i++) {
            pullToRefreshRecyclerView.removeHeaderView(i);
        }

        pullToRefreshRecyclerView.addHeaderView(getHeaderView(StringUtils.nullValue(scoreInfoVo.getTotalScore(), "0"), StringUtils.nullValue(scoreInfoVo.getFeeScore(), "0")));
    }


    /**
     * 获得一个顶部控件
     */
    public View getHeaderView(String title,String sub) {
        View header = LayoutInflater.from(this)
                                    .inflate(R.layout.item_score_head, (ViewGroup) findViewById(android.R.id.content), false);
        TextView titleView = (TextView) header.findViewById(R.id.tv_title);
        titleView.setText(title);
        TextView subTitleView = (TextView) header.findViewById(R.id.tv_sub_title);
        subTitleView.setText(sub);
        return header;
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_MY_SCORE);
    }
}
